package com.ljd.hosp.controller.api;

import com.ljd.hosp.repository.DepartmentRepository;
import com.ljd.hosp.service.DepartmentService;
import com.ljd.hosp.service.HospitalService;
import com.ljd.hosp.service.HospitalSetService;
import com.ljd.hosp.service.ScheduleService;
import com.ljd.yygh.exception.YyghException;
import com.ljd.yygh.helper.HttpRequestHelper;
import com.ljd.yygh.model.hosp.Department;
import com.ljd.yygh.model.hosp.Hospital;
import com.ljd.yygh.model.hosp.Schedule;
import com.ljd.yygh.result.Result;
import com.ljd.yygh.result.ResultCodeEnum;
import com.ljd.yygh.util.MD5;
import com.ljd.yygh.vo.hosp.DepartmentQueryVo;
import com.ljd.yygh.vo.hosp.ScheduleQueryVo;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.type.ErrorType;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.ljd.yygh.helper.HttpRequestHelper.switchMap;

@Api(tags = "医院管理api接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    ScheduleService scheduleService;
    @ApiOperation("查询医院接口")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    private Map<String, Object> switchMap(Map<String, String[]> parameterMap) {
        return HttpRequestHelper.switchMap(parameterMap);
    }

    @ApiOperation("上传医院接口")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        //logoData图片，进行base64编码后传输 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoData)) {
            String replaceAll = logoData.replaceAll(" ", "+");
            paramMap.put("logoData", replaceAll);
        }
        hospitalService.save(paramMap);
        return Result.ok();
    }
    @ApiOperation("上传科室接口")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        departmentService.saveDepartment(paramMap);
        return Result.ok();
    }
    @ApiOperation("删除科室接口")
    @PostMapping("/department/remove")
    public Result removeDept(HttpServletRequest request) {
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        String depcode = (String) paramMap.get("depcode");
        departmentService.removeDept(hoscode,depcode);
        return Result.ok();
    }
    @ApiOperation("查询科室接口")
    @PostMapping("/department/list")
    public Result listDept(HttpServletRequest request) {
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);

        Integer page = Integer.parseInt((String) paramMap.get("page"));
        Integer limit = Integer.parseInt((String) paramMap.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> departments = departmentService.listDept(page,limit,departmentQueryVo);
        return Result.ok(departments);
    }

    private void isRule(String hospSign, String hoscode) {
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);
        if(!hospSign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    @ApiOperation("/上传排班接口")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }
    @ApiOperation("/查询排班接口")
    @PostMapping("/schedule/list")
    public Result listSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        String depcode = (String) paramMap.get("depcode");
        Integer page = Integer.parseInt((String) paramMap.get("page"));
        Integer limit = Integer.parseInt((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> schedules = scheduleService.listSchedule(page,limit,scheduleQueryVo);
        return Result.ok(schedules);
    }
    @ApiOperation("/删除排版接口")
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = switchMap(request.getParameterMap());
        // 签名校验
        String hospSign = (String) paramMap.get("sign"); // 该签名是经过加密后的
        String hoscode = (String) paramMap.get("hoscode");
        isRule(hospSign,hoscode);
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.removeSchedule(hoscode,hosScheduleId);
        return Result.ok();
    }
}
